<?php
namespace app\api\controller;
use app\api\model\InviteRecord;
use app\api\model\PointsRecord;
use app\api\model\SignInRecord;
use think\Controller;
use think\Request;
use app\common\utils\Redis;

use app\api\model\User as UserModel;
use app\api\model\LoginModel;

use app\api\model\GoodsComment;
// 首页 信息
class User extends Common
{
    // 通知消息 数目显示
    public function getMessageNum(){

        $user_id = $this->checkLoginUser();
        $deadline = 604800; // 不提示 已经发布 7天的消息

        $num=\app\api\model\MessageModel::getUnreadMessageNum($user_id,$deadline);

        return $num;

    }

    // 通知消息 读取未读消息
    public function getUnreadMessage(){
        $user_id = $this->checkLoginUser();
        $deadline = 604800; // 不提示 已经发布 7天的消息

        $list = \app\api\model\MessageModel::getUnreadMessageNum($user_id, $deadline,true);

        return $list;

    }

    // 全部消息
    public function getAllMessage(){

    }

    // 添加消息已读记录
    public function getMessageReaded(){

    }

    //签到
    public function postSignIn(Request $request)
    {
        $user_id = $this->checkLoginUser(true);


        $res = SignInRecord::addRecord($user_id);
        if(!$res['status'])
        {
            to_json_encode(400,$res['msg'],['status'=>false]);

        }

        to_json_encode(200,'success',['status'=>true]);

    }
    
    public function postUserInfo(Request $request)
    {
        $user_id = $this->checkLoginUser(true);


        $info = UserModel::getUserInfo($user_id);

        $address_info = UserModel::getExtendInfo($user_id);
        to_json_encode(200,'success',[
            'user_name'=>$info['user_name'],
            'user_phone'=>$info['user_phone'],
            'address'=>empty($address_info)?'':$address_info['address']
        ]);
    }
    public function postSaveUserInfo(Request $request)
    {
        $user_id = $this->checkLoginUser(true);

        $user_name = $request->post('user_name','');
        $phone = $request->post('phone','');
        $address = $request->post('address','');


        if(empty($user_name) || empty($phone) || empty($address))
        {
            to_json_encode(400,'参数错误',['status'=>false]);

        }

        if(!check_valid_phone($phone))
        {
            to_json_encode(400, '手机号格式不正确', ['status' => false]);

        }

        $res = UserModel::saveUserInfo($user_id,$user_name,$phone,$address);
        if(!$res['status'])
        {
            to_json_encode(400,$res['msg'],['status'=>false]);

        }
        to_json_encode(200,'success',['status'=>true]);

    }
    //修改密码
    public function postModifyPassword(Request $request)
    {
        $user_id = $this->checkLoginUser(true);
        $phone = $request->post('phone');
        $password = $request->post('password','');

        $verify_code = $request->post('verify_code');


        if (!check_valid_phone($phone)) {
            to_json_encode(400, '手机号格式不正确', ['status' => false]);

        }
        if(empty($password))
        {
            to_json_encode(400, '密码不能为空', ['status' => false]);

        }

        $redis = Redis::instance();
        $sess_verify_code = $redis->get(Utility::$sms_template[3].'_'.$phone);

        if ($sess_verify_code != $verify_code) {
            to_json_encode(400, '验证码错误', ['status' => false]);
        }

        $password = encrypt_password($password);

        LoginModel::updateUserPassword($user_id,$password);
        to_json_encode(200, 'success',['status'=>true]);
    }

    public function getGoodsCommentList(Request $request)
    {
        $user_id = $this->checkLoginUser(true);
        $page = $request->param('page',1,'intval');

        to_json_encode(200,'success',GoodsComment::handleCommentList(
              GoodsComment::getUserCommentList($user_id,$page),true));

    }
    public function getInviteRecord(Request $request)
    {
        $user_id = $this->checkLoginUser(true);

        $page = $request->param('page',1,'intval');

        to_json_encode(200,'success',InviteRecord::handleInviteRecordList(
           InviteRecord::getUserInviteRecord($user_id,$page)
        ));
    }
    
    //当前月 签到日期数组
    public function getSignInDay(Request $request)
    {
        $user_id = $this->checkLoginUser();

        
        if($user_id)
        {
            to_json_encode(200,'success',SignInRecord::getSignInRecordDay($user_id));
        }else{
            to_json_encode(200,'success',[]);
        }
    }
    //捐赠积分
    public function postDonatePoints(Request $request)
    {
        $user_id = $this->checkLoginUser(true);

        $points = $request->post('points');

        if($points<=0)
        {
            to_json_encode(400, '参数错误', ['status' => false]);

        }
        $res = PointsRecord::donatePoints($user_id,$points);
        if(!$res['status'])
        {
            to_json_encode(400, $res['msg'], ['status' => false]);
        }
        to_json_encode(200,'success',['status' => true]);
    }
}
